// pages/myFollow/myFollow.js
var app = getApp();
const http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId: 0,
    isOpened: false,
    topicList:[],
    listText: '删除话题',
    myFollowArr:[]
  },

  //nav栏点击
  selectNav(e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      navId: id
    })

    if (id == 0) {
      this.getMyFollow();
    } else if (id == 1) {
      this.getFollowTopic(1);
    }

  },
  toTopicDetail(e) {
    var tid = e.currentTarget.dataset.tid;
    var state = e.currentTarget.dataset.state;
    if (state == 2) {
      wx.showToast({
        title: '无效的话题',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: `/pages/topicDetail/topicDetail?tid=${tid}`
    })
  },
  //更多操作
  moreOpt(e) {
    var sheet = this.selectComponent('#acsheet');
    var tid = e.currentTarget.dataset.tid;
    sheet.setData({
      tid
    })
    this.setData({
      isOpened: true,
     // listText: '删除话题'
    })
  },
  selectAction(e) {
    console.log(e.detail)
    this.setData({
      isOpened: false
    })

    //删除话题
    if (e.detail.idx == 1 && this.data.listText == '删除话题') {
      var tid = e.detail.tid
      var uid = app.globalData.uid;
      wx.showModal({
        title: '提示',
        content: '确定删除该话题?',
        success: (res1) => {
          if (res1.confirm) {
            http.fetchApi('procedure_t', { p: `R2019010|${tid}|${uid}` }).then(res => {
              if (res.data.code == 1) {
                if (res.data.results[0].codes == 1) {
                  wx.showToast({
                    title: '删除成功',
                  })
                  setTimeout(() => {
                    this.getFollowTopic(1)
                  }, 1000)
                } else {
                  wx.showToast({
                    title: '网络繁忙',
                    icon: 'none'
                  })
                }
              } else {
                wx.showToast({
                  title: '网络繁忙',
                  icon: 'none'
                })
              }
            })
          } else if (res1.cancel) {

          }
        }
      })



    }
  },
//取消关注
  cancelFolow(e){
    var oid=e.currentTarget.dataset.oid;
    var uid=app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2018001|${uid}|${oid}` }).then(res=>{
      if(res.data.code==1){
        wx.showToast({
          title: '取消关注',
        })
        setTimeout(()=>{
          this.getMyFollow();
        },1000)
      }
    })
  },
//去个人主页
  toPersonalPage(e){
    var eid=e.currentTarget.dataset.eid;
    wx.navigateTo({
      url: '/pages/toPersonal/toPersonal?eid='+eid
    })
  },

  //获取我关注的话题
  getFollowTopic(page1){
    wx.showLoading({
      title: '加载中',
    })
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2018009|${uid}|${page1}|10` }).then(res => {
      wx.hideLoading();
      var topicList = this.data.topicList;
      var topicList1 = res.data.results;
      if (page1 == 1) {
        topicList = res.data.results;
      } else {
        topicList = [...topicList, ...topicList1]
      }
      if (topicList1.length == 0 && page1 > 1) {
        http.showToast('暂无更多!' + '', '', 'none')
        return
      }
      this.setData({
        topicList,
        page1
      })
    })
  },

  //获取我关注的人
  getMyFollow(){
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: `R2019007|${uid}` }).then(res=>{
      if(res.data.code==1){
        var myFollowArr = res.data.results;
        this.setData({
          myFollowArr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyFollow()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.navId == 0) {
      // var page = this.data.page || 1;
      // page++;
      // this.getList(page);
    } else if (this.data.navId == 1) {
      var page1 = this.data.page1 || 1;
      page1++;
      this.getFollowTopic(page1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res)
   
    var uid = app.globalData.uid;
    var tid = res.target.dataset.tid;
    if (res.from === 'button') {
      // 来自页面内转发按钮

      http.fetchApi('procedure_t', { p: `R2017005|${tid}|${uid}` }).then(req => {
        if (req.data.code == 1) {
          if (req.data.results[0].codes == 1) {
           
          }
        }
      })
    }
    return {
      // title: '自定义转发标题',
      path: '/pages/topicDetail/topicDetail?tid=' + tid
    }
  }
})